import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_function.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_time_period.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/ext.dart';
import 'package:enterprise_v6_app/app/core/utils/customEndDrawer.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/education/couser_center/textbook_list_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/education/teacher_power/lecturer_list_page_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/get_new_dict_all_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/education/course_center/integrated_theme/integrated_theme_controller.dart';
import 'package:enterprise_v6_app/app/modules/application/education/education_controller.dart';

///主题
class IntegratedThemePage extends StatelessWidget {
  IntegratedThemePage(this.getDictAllEntity, {super.key});

  final GetNewDictAllEntity getDictAllEntity;
  final controller = Get.put(IntegratedThemeController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            key: getDictAllEntity.riskRightScaffoldKey,
            body: Container(
              color: AppColors.bg_color_grey,
              padding: EdgeInsets.only(top: 12.w, right: 16.w, left: 16.w),
              child: Column(
                children: [
                  Row(
                    children: [
                      Expanded(
                        child: _getSearchEnterWidget(),
                      ),
                      AppGaps.hGap8,
                      _screen(),
                      AppGaps.vGap10,
                    ],
                  ),
                  AppGaps.vGap12,
                  Expanded(
                      child: CustomEasyRefreshList<TextbookListEntity>(
                    key: getDictAllEntity.globalKey,
                    dataLoad: (int index, int size, {String? search}) {
                      return controller.textbookList(index, size, getDictAllEntity);
                    },
                    itemBuilder: (item, index) {
                      return _itemWidget(item);
                    },
                  )),
                ],
              ),
            ),
            // endDrawer: CustomEndDrawer(
            //   centerContext: SingleChildScrollView(
            //     child: Column(
            //       children: [
            //         _selectType(
            //           '分类主题',
            //           input: _input(),
            //         ),
            //         _selectType('讲师姓名'),
            //         Align(
            //           alignment: Alignment.centerLeft,
            //           child: Text(
            //             '培训时间',
            //             style: TextStyle(
            //               fontSize: 14.sp,
            //               fontWeight: FontWeight.w500,
            //             ),
            //           ),
            //         ),
            //         AppGaps.vGap16,
            //         CustomSelectTimePeriod(
            //           startTime: getDictAllEntity.startDate,
            //           endTime: getDictAllEntity.endDate,
            //           onConfirm: (start, end) {
            //             getDictAllEntity.startDate = start;
            //             getDictAllEntity.endDate = end;
            //           },
            //           showBorder: true,
            //           iconCalendar: AppImages.icon__screen_clock,
            //         ),
            //       ],
            //     ),
            //   ),
            //   reset: reset,
            //   confirm: confirm,
            // ),
          );
        });
  }

  //搜索框
  Widget _getSearchEnterWidget() {
    return Container(
        height: 38.w,
        padding: EdgeInsets.symmetric(horizontal: 12.w),
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: Colors.white, border: Border.all(color: const Color(0xffF0F0F0), width: 1.w), borderRadius: BorderRadius.all(Radius.circular(8.w))),
        child: Row(
          children: [
            SvgPicture.asset(
              AppImages.icon_search,
              width: 16.w,
            ),
            Expanded(
                child: TextField(
                    keyboardType: TextInputType.text,
                    textInputAction: TextInputAction.done,
                    controller: TextEditingController(text: getDictAllEntity.searchX.value),
                    onChanged: (value) {
                      getDictAllEntity.searchX.value = value;

                      getDictAllEntity.globalKey.currentState?.onRefresh();
                    },
                    style: TextStyle(color: const Color(0xff222222), fontWeight: FontWeight.w400, fontSize: 14.sp),
                    decoration: InputDecoration(
                      counterText: '',
                      //防止适配不居中
                      isCollapsed: true,
                      border: const OutlineInputBorder(borderSide: BorderSide.none),
                      contentPadding: EdgeInsets.only(left: 8.w),
                      hintText: '请输入课程名称搜索',
                      hintStyle: TextStyle(fontSize: 14.sp, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
                    )))
          ],
        ));
  }

  Widget _rowData(String name, String value, {Alignment alignment = Alignment.centerLeft}) {
    return Row(
      children: [
        Text(
          name,
          style: TextStyle(
            color: const Color(0xFFA5A6B5),
            fontWeight: FontWeight.w400,
            fontSize: 13.sp,
          ),
        ),
        Expanded(
            child: Align(
          alignment: alignment,
          child: Text(
            value,
            style: TextStyle(
              color: const Color(0xFF495061),
              fontSize: 12.sp,
              fontWeight: FontWeight.w400,
            ),
          ),
        )),
      ],
    );
  }

  ///分割线
  Widget _divider() {
    return Container(
      width: 2.w,
      height: 10.w,
      decoration: const BoxDecoration(
          gradient: LinearGradient(begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [
        Color(0xFFD8D8D8),
        Color(0xFFD8D8D8),
      ])),
    );
  }

  ///筛选
  Widget _screen() {
    return Container(
      width: 38.w,
      height: 38.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
      child: SvgPicture.asset(
        AppImages.icon_search_screen,
        width: 16.w,
      ),
    ).clickOnce(onFilterButtonPressed);
  }

  Widget _selectType(String firstName, {Widget? input}) {
    return Column(
      children: [
        Align(
          alignment: Alignment.centerLeft,
          child: Text(
            firstName,
            style: TextStyle(
              fontSize: 14.sp,
              fontWeight: FontWeight.w400,
              color: const Color(0xFF262F38),
            ),
          ),
        ),
        AppGaps.vGap16,
        input ??
            Container(
              height: 38.w,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(8),
                border: Border.all(width: 1.w, color: const Color(0xFFE3E7EE)),
              ),
              child: Row(
                children: [
                  AppGaps.hGap20,
                  Expanded(
                    child: Text(
                      _selectName(firstName),
                      style: TextStyle(
                        fontSize: 12.sp,
                        color: const Color(0xFFA0A7B5),
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                  ),
                  SvgPicture.asset(AppImages.icon_filter_right_arrow),
                  AppGaps.hGap20,
                ],
              ),
            ).clickOnce(() {
              if (firstName == '培训对象') {
                // _person();
              } else if (firstName == '讲师姓名') {
                _lecturerList();
              }
            }),
        AppGaps.vGap16,
      ],
    );
  }

  //筛选按钮
  void onFilterButtonPressed() {
    final EducationModController controller = Get.find<EducationModController>();
    controller.filterWidget.value = _filterWidget();
    controller.riskRightScaffoldKey.currentState?.openEndDrawer();
    getDictAllEntity.searchSelect = getDictAllEntity.searchX.value; //向另一个地方插入值 那个地方也和这一样
    // getDictAllEntity.riskRightScaffoldKey.currentState?.openEndDrawer();
  }

  void onChange(value) {
    getDictAllEntity.defaultValue = value;
    controller.update();
  }

  // 重置
  void reset() {
    getDictAllEntity.searchSelect = '';
    getDictAllEntity.workKindBack2 = '';
    getDictAllEntity.workKindNameBack2.value = '请选择';
    getDictAllEntity.searchX.value = '';
    getDictAllEntity.startDate = '';
    getDictAllEntity.endDate = '';
    getDictAllEntity.defaultValue = 0;
    getDictAllEntity.defaultValueSelect = 0;
    // getDictAllEntity.riskRightScaffoldKey.currentState?.closeEndDrawer();
    Get.find<EducationModController>().riskRightScaffoldKey.currentState?.closeEndDrawer();
    getDictAllEntity.globalKey.currentState!.onRefresh();
    controller.update();
  }

  // 确定
  void confirm() {
    Get.back();
    getDictAllEntity.searchX.value = getDictAllEntity.searchSelect;
    getDictAllEntity.defaultValueSelect = getDictAllEntity.defaultValue;
    getDictAllEntity.globalKey.currentState!.onRefresh();
    controller.update();
  }

  /// 教师姓名
  _lecturerList() {
    CustomSelectFunction.selectMultipleBottomSheet<LecturerListPageEntity>(
      title: '请选择',
      isMultiple: false,
      dataList: controller.lecturerList,
      checkedList: [getDictAllEntity.lecturerListPageEntity ?? LecturerListPageEntity()],
      onCallBack: (value) {
        getDictAllEntity.lecturerListPageEntity = value[0];
        getDictAllEntity.workKindBack2 = value[0].getId();
        getDictAllEntity.workKindNameBack2.value = value[0].getShowName();
        controller.update();
      },
    );
  }

  String _selectName(String firstName) {
    if (firstName == '培训对象') {
    } else if (firstName == '讲师姓名') {
      return getDictAllEntity.workKindNameBack2.value;
    }
    return '';
  }

  Widget _input() {
    return Container(
        height: 38.w,
        padding: EdgeInsets.symmetric(horizontal: 12.w),
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: Colors.white, border: Border.all(color: const Color(0xffF0F0F0), width: 1.w), borderRadius: BorderRadius.all(Radius.circular(8.w))),
        child: Row(
          children: [
            Expanded(
                child: Obx(() => TextField(
                    keyboardType: TextInputType.text,
                    textInputAction: TextInputAction.done,
                    controller:
                        TextEditingController(text: getDictAllEntity.searchX.value.isNotEmpty ? getDictAllEntity.searchX.value : getDictAllEntity.searchSelect),
                    onChanged: (value) {
                      getDictAllEntity.searchSelect = value;
                    },
                    style: TextStyle(color: const Color(0xff222222), fontWeight: FontWeight.w400, fontSize: 14.sp),
                    decoration: InputDecoration(
                      counterText: '',
                      //防止适配不居中
                      isCollapsed: true,
                      border: const OutlineInputBorder(borderSide: BorderSide.none),
                      contentPadding: EdgeInsets.only(left: 8.w),
                      hintText: '请输入课程名称',
                      hintStyle: TextStyle(fontSize: 12.sp, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
                    ))))
          ],
        ));
  }

  Widget _itemWidget(TextbookListEntity item) {
    return Container(
      padding: EdgeInsets.all(12.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8.w),
      ),
      child: Row(
        children: [
          item.textbookCover == null
              ? Container(
                  decoration: const BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.all(
                      Radius.circular(8),
                    ),
                  ),
                  width: 80.w,
                  height: 80.w,
                )
              : Container(
                  margin: EdgeInsets.only(top: 8.w, right: 8.w),
                  width: 80.w,
                  height: 80.w,
                  decoration: BoxDecoration(
                      borderRadius: const BorderRadius.all(
                        Radius.circular(8),
                      ),
                      color: Colors.white,
                      border: Border.all(
                        color: const Color(0xFFEFF2F6),
                        width: 1,
                      ),
                      image: DecorationImage(
                        image: NetworkImage('${Interface.fileUrl}${item.textbookCover}'),
                      )),
                )
          // .clickShowLarge(url: '${Interface.fileUrl}${item.textbookCover}')
          ,
          AppGaps.hGap8,
          Expanded(
              child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                item.textbookName ?? '',
                style: TextStyle(
                  color: const Color(0xFF262F38),
                  fontWeight: FontWeight.w500,
                  fontSize: 14.sp,
                ),
              ),
              AppGaps.vGap6,
              _rowData('讲师：', item.textbookLecturerName ?? ''),
              AppGaps.vGap6,
              Row(
                children: [
                  Expanded(
                    child: Row(
                      children: [
                        SvgPicture.asset(
                          AppImages.icon_course_clock,
                        ),
                        AppGaps.hGap4,
                        Text(
                          '学时数:',
                          style: TextStyle(
                            color: const Color(0xFFA5A6B5),
                            fontWeight: FontWeight.w400,
                            fontSize: 12.sp,
                          ),
                        ),
                        Expanded(
                          child: Text(
                            '${item.textbookApproAch ?? ''}',
                            style: TextStyle(
                              color: const Color(0xFF495061),
                              fontSize: 12.sp,
                              fontWeight: FontWeight.w400,
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                  Expanded(
                    child: Row(
                      children: [
                        _divider(),
                        AppGaps.hGap10,
                        SvgPicture.asset(
                          AppImages.icon_course_eye,
                        ),
                        AppGaps.hGap4,
                        Text(
                          '已学:',
                          style: TextStyle(
                            color: const Color(0xFFA5A6B5),
                            fontWeight: FontWeight.w400,
                            fontSize: 12.sp,
                          ),
                        ),
                        Expanded(
                          child: Text(
                            '${item.viewNumber ?? '0'}',
                            style: TextStyle(
                              color: const Color(0xFF495061),
                              fontSize: 12.sp,
                              fontWeight: FontWeight.w400,
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                ],
              )
            ],
          ))
        ],
      ),
    ).clickOnce(() {
      controller.courseDetail(item);
    });
  }

  Widget _filterWidget() {
    return CustomEndDrawer(
      centerContext: SingleChildScrollView(
        child: Column(
          children: [
            _selectType(
              '分类主题',
              input: _input(),
            ),
            Obx(() => _selectType('讲师姓名')),
            Align(
              alignment: Alignment.centerLeft,
              child: Text(
                '培训时间',
                style: TextStyle(
                  fontSize: 14.sp,
                  fontWeight: FontWeight.w500,
                ),
              ),
            ),
            AppGaps.vGap16,
            CustomSelectTimePeriod(
              startTime: getDictAllEntity.startDate,
              endTime: getDictAllEntity.endDate,
              onConfirm: (start, end) {
                getDictAllEntity.startDate = start;
                getDictAllEntity.endDate = end;
              },
              showBorder: true,
              iconCalendar: AppImages.icon__screen_clock,
            ),
          ],
        ),
      ),
      reset: reset,
      confirm: confirm,
    );
  }
}
